GetLeaveRequestByIdQueryHandler.execute   B
last analyzed

Complexity

Conditions 3

Size

Total Lines 51
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 51
rs 8.824
c 0
b 0
f 0
cc 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import { Inject } from '@nestjs/common';
2
import { QueryHandler } from '@nestjs/cqrs';
3
import { IDateUtils } from 'src/Application/IDateUtils';
4
import { LeaveRequestNotFoundException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestNotFoundException';
5
import { LeaveRequestRepository } from 'src/Infrastructure/HumanResource/Leave/Repository/LeaveRequestRepository';
6
import { UserSummaryView } from '../../User/View/UserSummaryView';
7
import { LeaveRequestDetailView } from '../View/LeaveRequestDetailView';
8
import { GetLeaveRequestByIdQuery } from './GetLeaveRequestByIdQuery';
9
import { CanLeaveRequestBeCancelled } from 'src/Domain/HumanResource/Leave/Specification/CanLeaveRequestBeCancelled';
10
11
@QueryHandler(GetLeaveRequestByIdQuery)
12
export class GetLeaveRequestByIdQueryHandler {
13
  constructor(
14
    @Inject('ILeaveRequestRepository')
15
    private readonly leaveRequestRepository: LeaveRequestRepository,
16
    @Inject('IDateUtils')
17
    private readonly dateUtils: IDateUtils,
18
    private readonly canLeaveRequestBeCancelled: CanLeaveRequestBeCancelled
19
  ) {}
20
21
  public async execute(
22
    query: GetLeaveRequestByIdQuery
23
  ): Promise<LeaveRequestDetailView> {
24
    const leaveRequest = await this.leaveRequestRepository.findOneById(
25
      query.id
26
    );
27
28
    if (!leaveRequest) {
29
      throw new LeaveRequestNotFoundException();
30
    }
31
32
    const user = leaveRequest.getUser();
33
    const moderator = leaveRequest.getModerator();
34
    let moderatorView: UserSummaryView;
35
36
    if (moderator) {
37
      moderatorView = new UserSummaryView(
38
        moderator.getId(),
39
        moderator.getFirstName(),
40
        moderator.getLastName()
41
      );
42
    }
43
44
    return new LeaveRequestDetailView(
45
      leaveRequest.getId(),
46
      leaveRequest.getType(),
47
      leaveRequest.getStatus(),
48
      leaveRequest.getStartDate(),
49
      leaveRequest.isStartsAllDay(),
50
      leaveRequest.getEndDate(),
51
      leaveRequest.isEndsAllDay(),
52
      this.dateUtils.getLeaveDuration(
53
        leaveRequest.getStartDate(),
54
        leaveRequest.isStartsAllDay(),
55
        leaveRequest.getEndDate(),
56
        leaveRequest.isEndsAllDay()
57
      ),
58
      this.canLeaveRequestBeCancelled.isSatisfiedBy(
59
        query.currentUser,
60
        leaveRequest
61
      ),
62
      leaveRequest.getComment(),
63
      new UserSummaryView(
64
        user.getId(),
65
        user.getFirstName(),
66
        user.getLastName()
67
      ),
68
      moderatorView,
69
      leaveRequest.getModerateAt(),
70
      leaveRequest.getModerationComment()
71
    );
72
  }
73
}
74